import { AdvVisitor } from "./adv";
import { Ast, createRootAst, uploadXml } from "./ast";
import { PACKAGES } from "./utils";
if (!AdvVisitor) {
    throw new Error(`发生了循环依赖`)
}
class CloseAppContext {
    close: () => void;
    status: 'finish' | 'next' | 'open' | 'clear' | string;
}
class CloseAppVisitor extends AdvVisitor {
    visitLinearLayout(node: Ast, ctx: CloseAppContext) {
        if (node.desc === `结束运行`) {
            ctx.close = () => {
                node.click()
            }
            return;
        }
        node.children.map(c => c.visit(this, ctx))
    }
    visitButton(node: Ast, ctx: CloseAppContext) {
        if (node.sid === `middle_button` && node.text === `强行停止`) {
            ctx.close = () => {
                node.click()
            }
            return;
        }
        if (node.text === `结束运行`) {
            ctx.close = () => {
                node.click()
            }
            return;
        }
        if (node.sid === `right_button` && node.text === `强行停止`) {
            ctx.close = () => {
                node.click()
            }
            return;
        }
        if (node.sid === `button1` && (node.text === `确定` || node.text === `强行停止`)) {
            ctx.close = () => {
                node.click()
                ctx.status = 'finish'
            }
            return;
        }
    }
}
export function _launchPackage(currentPkg: string) {
    let success = false;
    const root = createRootAst()
    success = root?.packageName == currentPkg
    while (!success) {
        const root = createRootAst()
        success = root?.packageName == currentPkg
        if (!success) {
            toastLog(`正在打开${currentPkg}`)
            launchPackage(currentPkg)
            selector().text('允许').findOne(1000 * 10)?.click()
        }
    }
    toastLog(`应用已打开`)
}
const isRuning = (that?: any) => {
    if (!that) return true;
    if (that.isRuning && typeof that.isRuning == 'function') return that.isRuning();
    if (that.isRuning) return that.isRuning;
    if (that.isExit) return false;
    return true;
}
function currentPpkg() {
    const root = createRootAst()
    return root.packageName;
}
export function closeApp2(packageName: string) {
    while (currentPpkg() === packageName) {
        back();
        back();
    };
}
export const closeApp = (pkg: string = PACKAGES.KUAI_SHOU, that?: any) => {
    sleep(1000)
    home()
    sleep(1000)
    while (!openAppSetting(pkg) && isRuning(that)) {
        toastLog(`找不到${pkg}应用`)
        sleep(1000)
    }
    toastLog(`详情已打开`)
    const ctx: CloseAppContext = new CloseAppContext()
    const visitor = new CloseAppVisitor()
    let max = 30;
    while (ctx.status !== 'finish' && max > 0 && isRuning(that)) {
        const root = createRootAst()
        visitor.visitNode(root, ctx)
        if (ctx.close) {
            ctx.close()
            ctx.close = undefined;
        }
        sleep(1000)
        max -= 1;
    }
    if (ctx.status !== 'finish') {
        uploadXml(`debug`, 'close')
        return;
    }
    sleep(1000)
    back();
    sleep(1000)
    back();
    sleep(1000)
    back();
    sleep(1000)
    home();
    sleep(1000)
}
